Repository navigation
Refactor DiffBuilder to use tuple paths and simplify operation handling - #197
Merged
Merged
Conversation
When the diff pairs a removal with a later addition of an equal value (or vice versa) it replaces them with a move, and has to adjust the locations of all operations in between. That bookkeeping had several gaps, so make_patch still produced patches that fail to apply or give a wrong result: - the moved value's location was only tracked when both the removal and the addition were array elements, and only in its last part, so shifts of a parent array index were missed - operations nested inside the array were shifted with the rules for inserting and removing at the array itself, and replace operations were not shifted at all - object keys like "0" were shifted like array indices (and could raise a TypeError) The DiffBuilder now keeps locations as tuples of object keys (str) and array indices (int) and adjusts the operations in between and the moved value's location with one set of rules. It no longer creates a move into a child of its 'from' location, which RFC 6902 does not allow. The _on_undo_* hooks on the operation classes are no longer needed and are removed. Out of random array diffs that used to fail about 1.5% of the time, none fail now, and the generated patches have the same size on average. The examples for this in property_tests.py pass, so they are removed from the expected failures, and the "safe" documents now include nested arrays and numeric object keys. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gfq7r4PD7upqMjnucEgSrM
…0xzvy2 # Conflicts: # jsonpatch.py # property_tests.py
The example from #160 is a value moved from an array into an object member, which failed for some key orders. The docstring of test_issue_138 referred to the removed _on_undo methods. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gfq7r4PD7upqMjnucEgSrM
…0xzvy2 #173 fixed #160 within the _on_undo bookkeeping that this branch replaces, so the branch's _item_added and _item_removed are kept. The duplicate test for #160 is folded into the test_issue_160 from master. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gfq7r4PD7upqMjnucEgSrM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the JSON patch diff generation to use tuple-based paths internally instead of string paths, and simplifies operation handling by working with dictionaries instead of operation objects. This enables more robust array index adjustment when generating move operations.
Key Changes
Path representation: Changed internal path representation from strings (e.g.,
'/a/0/b') to tuples (e.g.,('a', 0, 'b')) to distinguish between object keys (strings) and array indices (integers)Operation handling: Replaced
PatchOperationsubclass instances with plain dictionaries throughoutDiffBuilder, simplifying the code and reducing object instantiation overheadRemoved methods: Eliminated
_on_undo_add(),_on_undo_remove(),_increment_part(), and_decrement_part()methods from operation classes as they are no longer needed with the new approachNew path adjustment functions: Added helper functions to properly adjust array indices when operations affect items:
_with_item(): Adjusts paths when an item is added_without_item(): Adjusts paths when an item is removed_item_after(): Tracks where an item ends up after operations_shift(): Shifts array indices by an offsetImproved move operation generation: Rewrote
_item_added()and_item_removed()to use the new_adjust_following()method, which properly adjusts subsequent operations' paths when a move is generatedRFC 6902 compliance: Added validation to prevent moves where the destination is inside the source path (RFC 6902 section 4.4)
Test updates: Added comprehensive test cases for move operations with complex nested structures and numeric object keys
Implementation Details
The key insight is that by maintaining paths as tuples during diff generation, we can reliably distinguish between array indices that need adjustment and object keys that don't. When a move operation is generated, the code now properly adjusts all following operations' paths to account for the removal and re-insertion of the moved item, ensuring the patch applies correctly to the original document.
https://claude.ai/code/session_01Gfq7r4PD7upqMjnucEgSrM
fixes #179